package com.htfyun.roobofactory.engineerTest;

import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.widget.TextView;

import com.htfyun.roobofactory.BaseFragment;
import com.htfyun.roobofactory.R;
import com.htfyun.roobofactory.TestKeysAttr;
import com.roobo.hardware.KeyEventManager;

/**
 * Created by sst on 2018/7/11.
 */

public class EngineerKeyFragment extends BaseFragment implements KeyEvent.Callback {


    private KeyEventManager keyEventManager = KeyEventManager.getInstance();

    private GridView gridView;
    private KeyAdapter keyAdapter;

    private TextView txtKeyCode;


    private int flagKeyDownTest = 0;
    private int flagKeyUpTest = 0;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_engineer_keys;
    }

    @Override
    protected void initView(View view, Bundle savedInstanceState) {

        txtKeyCode = (TextView) view.findViewById(R.id.txtKeyCode);
        gridView = (GridView) view.findViewById(R.id.gridView);

        keyAdapter = new KeyAdapter(getContext(), -1, TestKeysAttr.values());

        gridView.setAdapter(keyAdapter);

        for (TestKeysAttr attr : TestKeysAttr.values()) {
            flagKeyDownTest |= attr.getFlagKeyDown();
            flagKeyUpTest |= attr.getFlagKeyUp();
        }
    }


    @Override
    public void onAttach(Context context) {
        super.onAttach(context);

        keyEventManager.registerKeyEventListener(this, null);

    }

    @Override
    public void onDetach() {
        super.onDetach();

        keyEventManager.unregisterKeyEventListener(this);

    }

    @Override
    protected void initData() {


    }

    private TestKeysAttr getKeyAttrByKeyCode(final int keyCode) {
        for (TestKeysAttr attr : TestKeysAttr.values()) {
            if (attr.getKeyCode() == keyCode) {
                return attr;
            }
        }
        return null;
    }
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        String status = getString(R.string.engineer_test_current_keycode_status_down);
        String name = "null";
        int pinLevel = 0;
        KeyEventManager.RK_KEYS rooboKeysAttr = keyEventManager.getRKKeysByKeyCode((keyCode));
        if (rooboKeysAttr != null) {
            name = rooboKeysAttr.getKeyLabel();
            pinLevel = keyEventManager.getKeyState(rooboKeysAttr) ? 1 : 0;
        }
        txtKeyCode.setText(getString(R.string.engineer_test_current_keycode, keyCode, status, name, pinLevel));

        TestKeysAttr attr = getKeyAttrByKeyCode(keyCode);
        if (attr != null) {
            flagKeyDownTest &= ~attr.getFlagKeyDown();

            keyAdapter.notifyDataSetChanged();

        }

        event.startTracking();

        Log.e(TAG, "getRepeatCount = " + event.getRepeatCount() + ", isLongPress = " + event.isLongPress());

        return true;
    }

    @Override
    public boolean onKeyLongPress(int keyCode, KeyEvent event) {

        Log.e(TAG, "!!!!!!!!! onKeyLongPress = " + keyCode);

        String status = getString(R.string.engineer_test_current_keycode_status_down_long);
        String name = "null";
        int pinLevel = 0;
        KeyEventManager.RK_KEYS rooboKeysAttr = keyEventManager.getRKKeysByKeyCode((keyCode));
        if (rooboKeysAttr != null) {
            name = rooboKeysAttr.getKeyLabel();
            pinLevel = keyEventManager.getKeyState(rooboKeysAttr) ? 1 : 0;
        }
        txtKeyCode.setText(getString(R.string.engineer_test_current_keycode, keyCode, status, name, pinLevel));

        return true;
    }


    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {

        String status = getString(R.string.engineer_test_current_keycode_status_up);
        String name = "null";
        int pinLevel = 0;
        KeyEventManager.RK_KEYS rooboKeysAttr = keyEventManager.getRKKeysByKeyCode((keyCode));
        if (rooboKeysAttr != null) {
            name = rooboKeysAttr.getKeyLabel();
            pinLevel = keyEventManager.getKeyState(rooboKeysAttr) ? 1 : 0;
        }
        txtKeyCode.setText(getString(R.string.engineer_test_current_keycode, keyCode, status, name, pinLevel));


        TestKeysAttr attr = getKeyAttrByKeyCode(keyCode);
        if (attr != null) {
            flagKeyDownTest |= attr.getFlagKeyDown();
            flagKeyUpTest &= ~attr.getFlagKeyUp();

            keyAdapter.notifyDataSetChanged();

            return true;
        }
        return false;
    }

    @Override
    public boolean onKeyMultiple(int keyCode, int count, KeyEvent event) {
        return false;
    }


    private final class KeyAdapter extends ArrayAdapter<TestKeysAttr> {


        public KeyAdapter(@NonNull Context context, int resource, @NonNull TestKeysAttr[] objects) {
            super(context, resource, objects);
        }

        @NonNull
        @Override
        public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
            ViewHolder holder = null;
            if (convertView != null) {
                holder = (ViewHolder) convertView.getTag();
            } else {
                holder = new ViewHolder();
                LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                convertView = inflater.inflate(R.layout.item_keys, null);
                holder.txt = (TextView) convertView.findViewById(R.id.txtKeys);

                convertView.setTag(holder);
            }

            TestKeysAttr attr = getItem(position);
            if (attr != null) {
                if ((flagKeyDownTest & attr.getFlagKeyDown()) == 0) {

                    holder.txt.setBackgroundColor(Color.YELLOW);//getColor(R.color.colormYellow));

//                } else  if ((flagKeyUpTest & attr.getFlagKeyDown()) == 0) {
//
//                    holder.txt.setBackgroundColor(getContext().getColor(R.color.colorGreen));

                } else {

                    holder.txt.setBackgroundColor(getContext().getColor(R.color.transparent));

                }

                String keyInfo = getString(attr.getNameResId());
                keyInfo += getString(R.string.arrow) + attr.getKeyCode();

                holder.txt.setText(keyInfo);
            }
            return convertView;
        }
    }

    private final class ViewHolder {
        TextView txt;
    }


}
